<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Validation\Annotation\Scene;

use App\Service\SysRole as SysRoleService;
use App\Request\SysRoleRequest;

#[AutoController]
class SysRoleController extends AbstractController
{
    // 角色列表
    public function role_list()
    {
        $map = [];
        $role_name = $this->request->input('role_name', '');
        if ($role_name) {
            $map[] = ['role_name', '=', $role_name];
        }

        $result = (new SysRoleService())->roleList($map);
        return $this->returnSuccess($result, '成功');
    }

    // 删除角色
    public function delete_role()
    {
        $role_id = $this->request->input('role_id', 0);
        if (!$role_id)
            return $this->returnError('请求参数错误');
        $result = (new SysRoleService())->deleteRole($role_id);
        return $this->returnSuccess($result, '删除成功');
    }

    // 添加角色
    #[Scene(scene: 'add', argument: 'request')]
    public function add_role(SysRoleRequest $request)
    {
        $params = $request->validated();
        print_r($params);
        $data = (new SysRoleService())->addSysRole($params);
        return $this->returnSuccess($data, '添加成功');
    }

    // 编辑角色
    #[Scene(scene: 'edit', argument: 'request')]
    public function edit_role(SysRoleRequest $request)
    {
        $params = $request->validated();
        $data = (new SysRoleService())->editSysRole($params['role_id'], $params);
        return $this->returnSuccess($data, '编辑成功');
    }
}
